package cc.customer.MRecord.service.Impl;

import cc.customer.MRecord.service.MRecordService;
import cc.customer.common.domain.MRecord;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.DateUtil;
import cc.customer.common.utils.Utils;
import cc.customer.cusDynamic.dao.CusDynamicMapper;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.MRecord.dao.MRecordMapper;
import cc.customer.system.domain.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @MethodName: $
 * @Description: TODO
 * @Param: $
 * @Return: $
 * @Author: cunfeng
 * @Date: 2020/6/17 17:05
 **/

@Service
public class MRecordServiceImpl extends BaseService<MRecord> implements MRecordService {

    @Autowired
    MRecordMapper mRecordMapper;

    @Autowired
    private CusDynamicMapper cusDynamicMapper;


    @Override
    public ResponseBo addMRecord(MRecord mRecord,String userid,Integer status) {
        try {


            mRecord.setCreatTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            CusDynamic cusDynamic = new CusDynamic();
            cusDynamic.setDyCreator(userid);
            cusDynamic.setDySourceType(mRecord.getmSourceType());
            cusDynamic.setDyType("22");
            cusDynamic.setDyCusUuid(mRecord.getmCusuuid());

            if(status == 1){
                cusDynamic.setDyFollowContent("手机拨打电话已接通");
                if(StringUtils.isBlank(mRecord.getmPath())){
                    cusDynamic.setDyFollowContent("手机拨打电话未接通");
                }
            }else if(status == 2){
                cusDynamic.setDyFollowContent("手机拨打电话已接通");
                if(StringUtils.isBlank(mRecord.getmPath())){
                    cusDynamic.setDyFollowContent("手机拨打电话未接通或者未上传录音");
                }
            }

            cusDynamic.setDyContent("手机拨打电话");
            cusDynamic.setDyCreattime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            cusDynamic.setDyUuid(Utils.getUUID());
            int row = mRecordMapper.insertSelective(mRecord);
            if(row == 1){
                cusDynamic.setDyMId(mRecord.getmId());
                cusDynamicMapper.insertSelective(cusDynamic);
                return   ResponseBo.ok("添加成功");
            }
            return   ResponseBo.error("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return   ResponseBo.error("系统错误");
        }
    }

    /**
     *沟通记录列表
     * @param mRecord
     * @param users
     * @return
     */
    @Override
    public List<MRecord> selectMRecord(MRecord mRecord, MyUser users) {
        try {
            /**时间格式化选择**/
            String starttime = mRecord.getBeforeTime();
            if("1".equals(starttime)){
                Date week = DateUtil.addDays(new Date(),-0);
                String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
                mRecord.setBeforeTime(datestr);
                mRecord.setAfterTime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
            }else if("2".equals(starttime)){
                Date week = DateUtil.addDays(new Date(),-1);
                String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
                 mRecord.setBeforeTime(datestr);
                 mRecord.setAfterTime(DateUtil.convert2String(week,"yyyy-MM-dd")+" 23:59:59");
            }else if("7".equals(starttime)){
                Date week = DateUtil.addDays(new Date(),-7);
                String datestr = DateUtil.convert2String(week,"yyyy-MM-dd")+" 00:00:00";
                 mRecord.setBeforeTime(datestr);
                 mRecord.setAfterTime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
            }else if ("30".equals(starttime)){
                Date month = DateUtil.addDays(new Date(),-30);
                String datestr = DateUtil.convert2String(month,"yyyy-MM-dd")+" 00:00:00";
                 mRecord.setBeforeTime(datestr);
                 mRecord.setAfterTime(DateUtil.getCurrentDate("yyyy-MM-dd")+" 23:59:59");
            }else{
                /**时间格式化**/
                String endtime = mRecord.getAfterTime();
                if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
                    String timestart = starttime+" 00:00:00";
                    String timeend = endtime+" 23:59:59";
                     mRecord.setBeforeTime(timestart);
                     mRecord.setAfterTime(timeend);
                }
            }
            return mRecordMapper.selectMRecordList(mRecord);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
